﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for AddSignProjectSiteKeyInService
    /// </summary>
    public class AddSignProjectSiteKeyInService : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            var model = new OTS_SITE();
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            if (request["oper"] == "add")
            {              
                model.NAME = request["NAME"];
                model.TIMESHEET_FORMAT = request["TIMESHEET_FORMAT"];
                model.STATUS = true;
                model.CREATED = DateTime.Now;
                model.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                var cmd = new Site_Service(model);
                cmd.Add();
            }
            else
            {
                if (request["oper"] == "del")
                {
                    model.ID = Convert.ToInt16(request["ID"]);
                    var cmd = new Site_Service(model);
                    cmd.Delete();
                }
                else if (request["oper"] == "edit")
                {
                    model.ID = Convert.ToInt16(request["ID"]);
                    model.TIMESHEET_FORMAT = request["TIMESHEET_FORMAT"];
                    model.NAME = request["NAME"];                    
                    var cmd = new Site_Service(model);
                    cmd.Edit();
                }
            }

            var _cmd = new Site_Service();
            var lst = new List<OTS_SITE>();
            var row = 0;
            foreach (OTS_SITE d in _cmd.GetAll().OrderByDescending(x=> x.CREATED).ToList())
            {
                row = row + 1;
                model = new OTS_SITE();
                model.ID = d.ID;
                model.RowIndex = row;
                model.NAME = d.NAME;
                model.TIMESHEET_FORMAT = d.TIMESHEET_FORMAT;
                model.FORMAT_NAME = getTimesheetFormat(d.TIMESHEET_FORMAT);
                lst.Add(model);
            }
            lst = lst.OrderBy(o => o.RowIndex).ToList();


            int total = lst.Count == 0 ? 1 : (int)Math.Ceiling((double)lst.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = lst.Count,
                rows = lst.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }
        public string getTimesheetFormat(string id)
        {
            if (id== "1")
            {
                return "BOT";
            }
            else if (id == "2")
            {
                return "CUBE";
            }
            else
            {
                return "TBANK";
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}